<template>
  <div class="hospitalsign">
    <div class="top">
      <div class="title">住院信息</div>
      <div class="topinfo">
        <div class="post">医院名称</div>
        <div>
          <input
            type="text"
            placeholder="请输入医院名称"
            v-model="form.yymc"
          />
        </div>
      </div>
      <div class="topinfo" @click="showDatePicker('zyrq')">
        <div class="post">住院日期</div>
        <div style="padding-left:10px" :style="form.zyrq ? '' : 'color:#ccc;'">
          {{form.zyrq | dateFormat('请选择住院日期')}}
        </div>
      </div>
      <div class="topinfo">
        <div class="post">入院诊断</div>
        <div>
          <input
            type="text"
            placeholder="请输入入院诊断"
            v-model="form.ryzd"
          />
        </div>
      </div>
      <div class="topinfo">
        <div class="post special">是否需要支票（{{zbjb === '1' ? '2-10' : '3-5'}}万）</div>
        <div class="arrow" @click="showPicker('needzp')">
          <p :style="{ color: needzp ? '#333333' : '#cccccc' }">
            {{ needzp ? needzp : "请选择" }}
          </p>
          <van-icon name="arrow" />
        </div>
      </div>
    </div>
    <div class="sign-btn" @click="sign">登记</div>
    <div class="historylist">
      <p class="historytitle">住院登记记录</p>
      <div class="item" @click="loadDetail(item.zyxxId)" v-for="(item, i) in list" :key="i">
        <div>
          <p>住院日期：{{ item.zyrq }}</p>
          <p>医院名称：{{ item.yymc }}</p>
        </div>
        <div><van-icon name="arrow" /></div>
      </div>
    </div>
    <date-picker
      @fetchData="fetchDate"
      date-type="date"
      :default-date="form.date"
      :show="isDatePickerShow"
      @cancel="hideDatePicker"
      @hide="hideDatePicker"
    ></date-picker>
    <picker
      title="请选择"
      :columns="pickerOptions.pickerColumns"
      @fetchData="setData"
      :show="show"
      @cancel="hidePicker"
      @hide="hidePicker"
    ></picker>
  </div>
</template>

<script>
import picker from '../components/picker';
import moment from 'moment';
import datePicker from '../components/datePicker';
import {zyxxsavedata, zyxxQuerydata4object, zyxxQuerydata4list, zyxxQuerydata} from '../assets/js/api';
import {Dialog} from 'vant';
export default {
  filters: {
    dateFormat(v, defaultValue) {
      return (v && moment(v).format('YYYY-MM-DD')) || defaultValue;
    }
  },
  data() {
    return {
      form: {
        yymc: '',
        zyrq: '',
        ryzd: '',
        sfxyzp: ''
      },
      dspToken: localStorage.dspToken,
      zbjb: '',
      isDatePickerShow: false,

      needzp: '',
      needzpArr: [
        {
          label: '是',
          value: 1
        },
        {
          label: '否',
          value: 0
        }
      ],
      show: false,
      pickerOptions: {
        pickerColumns: []
      },
      list: []
    };
  },
  components: {
    picker,
    datePicker
  },
  mounted() {
    zyxxQuerydata4object({
      dspToken: this.dspToken
    }).then(res => {
      this.zbjb = res.data[0][0].zbjb;
    });
    this.getZyxxQuerydata4list();
  },
  methods: {
    getZyxxQuerydata4list() {
      zyxxQuerydata4list({
        dspToken: this.dspToken
      }).then(res => {
        this.list = res.data;
      });
    },
    showDatePicker(type) {
      this.isDatePickerShow = true;
    },
    fetchDate(d) {
      this.form.zyrq = d.originValue;
      this.isDatePickerShow = d.showDatePicker;
    },
    hideDatePicker(d) {
      this.isDatePickerShow = d.showDatePicker;
    },
    loadDetail(id) {
      if (id) {
        zyxxQuerydata(id).then(({data}) => {
          const o = data[0];
          this.$router.push({
            name: 'detail',
            query: {
              title: '住院登记详情',
              data: JSON.stringify({
                '医院名称': o.yymc,
                '住院日期': o.zyrq,
                '入院诊断': o.ryzd,
                '是否需要支票': o.sfxyzp
              })
            }
          });
        });
      }
    },
    showPicker(type) {
      this.type = type;
      this.show = true;
      if (type === 'needzp') {
        this.pickerOptions.pickerColumns = this.needzpArr;
      }
    },
    hidePicker(d) {
      this.show = d.showPicker;
    },
    setData(d) {
      if (this.type === 'needzp') {
        this.needzp = d.value.label;
        this.form.sfxyzp = d.value.value;
      }
      this.show = d.showPicker;
    },
    sign() {
      zyxxsavedata({
        ...this.form,
        dspToken: this.dspToken,
        zyrq: moment(this.form.zyrq).format('YYYY-MM-DD')
      }).then(res => {
        if (this.form.sfxyzp === 1) {
          Dialog.alert({
            message: this.zbjb === '1' ? '请携带住院通知单（盖章），到二处办理，联系电话63098077。' : '请携带住院通知单（盖章），到三处办理，联系电话83085422'
          }).then(() => {
            this.form = {
              yymc: '',
              zyrq: '',
              ryzd: '',
              sfxyzp: ''
            };
            this.needzp = '';
            this.getZyxxQuerydata4list();
            this.$toast('登记成功');
          });
        } else {
          this.form = {
            yymc: '',
            zyrq: '',
            ryzd: '',
            sfxyzp: ''
          };
          this.needzp = '';
          this.getZyxxQuerydata4list();
          this.$toast('登记成功');
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.hospitalsign {
  padding: 24px;
  .top {
    width: 704px;
    background-color: #ffffff;
    box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
    border-radius: 8px;
    margin: 0 auto;
    .title {
      height: 64px;
      background-color: #3198ed;
      color: #ffffff;
      font-size: 30px;
      line-height: 64px;
      padding: 0 16px;
      border-top-left-radius: 8px;
      border-top-right-radius: 8px;
    }
    .topinfo {
      display: flex;
      padding: 35px 18px;
      box-sizing: border-box;
      width: 674px;
      margin: 0 auto;
      font-size: 34px;
      color: #343434;
      &:not(:last-child) {
        border-bottom: 1px solid #eeeeee;
      }
      .post {
        color: #888888;
        text-align: left;
        width: 200px;
      }
      div:last-child {
        flex: 1;
        text-align: right;
        input {
          text-align: right;
        }
      }
      .special {
        width: 380px;
      }
      .arrow {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        font-size: 34px;
        color: #888;
        text-align: right;
      }
    }
  }
  .sign-btn {
    border: none;
    width: 704px;
    height: 90px;
    background-color: #3198ed;
    box-shadow: 0px 5px 5px 0px rgba(89, 166, 250, 0.1);
    border-radius: 45px;
    line-height: 90px;
    color: #ffffff;
    font-size: 36px;
    text-align: center;
    margin: 26px auto;
    letter-spacing: 7px;
  }
  .historylist {
    .historytitle {
      color: #888888;
      font-size: 30px;
      margin: 19px 0;
    }
    .item {
      width: 704px;
      // height: 140px;
      padding: 36px 0;
      background-color: #ffffff;
      box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
      border-radius: 8px;
      margin: 0 auto;
      margin-top: 23px;
      position: relative;
      padding: 31px 30px 26px 33px;
      box-sizing: border-box;
      display: flex;
      &::before {
        content: "";
        width: 5px;
        // height: 140px;
        background-color: #3198ed;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        border-top-left-radius: 50px;
        border-bottom-left-radius: 50px;
      }
        > div:first-child {
        flex: 1;
        p:first-child {
          color: #888888;
          font-size: 30px;
        }
        p:last-child {
          width: calc(100vw - 160px);
          color: #353535;
          font-size: 34px;
          // white-space: nowrap;
          // text-overflow: ellipsis;
          // overflow: hidden;
        }
      }
      > div:last-child {
        width: 50px;
        font-size: 34px;
        color: #b2b2b2;
        align-self: center;
        text-align: right;
      }
    }
  }
}
</style>
